﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThouShaltNotBrick.Logic.GeneralObjects;
using ThouShaltNotBrick.Logic.Resources;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.Helpers
{
    public sealed class LevelDrawer
    {
        #region Properties
        private GraphicsDeviceManager mGraphics;
        private GraphicsResources mGraphicsResources;
        private FontResources mFontResources;
        private SpriteBatch mSpriteBatch;

        private float mScreenWidth;
        private float mScreenHeight;
        #endregion

        #region Constructors
        public LevelDrawer(
            GraphicsDeviceManager pGraphics,
            GraphicsResources pGraphicsResources,
            FontResources pFontResources,
            float pScreenWidth,
            float pScreenHeight)
        {
            mGraphics = pGraphics;
            mGraphicsResources = pGraphicsResources;
            mFontResources = pFontResources;
            mSpriteBatch = new SpriteBatch(mGraphics.GraphicsDevice);

            mScreenWidth = pScreenWidth;
            mScreenHeight = pScreenHeight;
        }
        #endregion

        #region Methods
        public void Draw(
            GameTime pGameTime,
            PlayData pPlayData)
        {
            LevelWithCurrentState currentLevel = pPlayData.CurrentLevel;

            mGraphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            mSpriteBatch.Begin();
            DrawPad(currentLevel.Pad);
            DrawBall(pGameTime, currentLevel.Balls[0]);
            DrawBricks(currentLevel.Bricks);
            mSpriteBatch.End();
        }

        private void DrawPad(Pad pPad)
        {
            Rectangle drawingRectangle = DrawingHelpers.GetPadDrawingRectangle(
                pPad,
                mScreenWidth,
                mScreenHeight);

            mSpriteBatch.Draw(mGraphicsResources.PadTexture, drawingRectangle, Color.White);
        }

        private void DrawBall(GameTime pGameTime, Ball pBall)
        {
            Rectangle drawingRectangle = DrawingHelpers.GetBallDrawingRectangle(
                pBall,
                (float)pGameTime.ElapsedGameTime.TotalSeconds,
                mScreenWidth,
                mScreenHeight);

            mSpriteBatch.Draw(mGraphicsResources.BallTexture, drawingRectangle, Color.White);
        }

        private void DrawBricks(BrickWithCurrentState[,] pBricks)
        {
            for (int i = 0; i < Constants.BRICK_ROWS; i++)
            {
                for (int j = 0; j < Constants.BRICK_COLUMNS; j++)
                {
                    BrickWithCurrentState brick = pBricks[i, j];
                    if (brick != null)
                    {
                        DrawBrick(brick);
                    }
                }
            }
        }

        private void DrawBrick(BrickWithCurrentState pBrick)
        {
            Rectangle drawingRectangle = DrawingHelpers.GetBrickDrawingRectangle(
                pBrick,
                mScreenWidth,
                mScreenHeight);

            Texture2D brickTexture = pBrick.GetCurrentBrickStateTexture();
            if (brickTexture == null)
            {
                return;
            }

            mSpriteBatch.Draw(brickTexture, drawingRectangle, Color.White);
        }
        #endregion
    }
}